﻿/*
*      
*          ┌─┐       ┌─┐
*       ┌──┘ ┴───────┘ ┴──┐
*       │                 │
*       │       ───       │
*       │  ─┬┘       └┬─  │
*       │                 │
*       │       ─┴─       │
*       │                 │
*       └───┐         ┌───┘
*           │         │
*           │         │
*           │         │
*           │         └──────────────┐
*           │                        │
*           │                        ├─┐
*           │                        ┌─┘    
*           │                        │
*           └─┐  ┐  ┌───────┬──┐  ┌──┘         
*             │ ─┤ ─┤       │ ─┤ ─┤         
*             └──┴──┘       └──┴──┘ 
*                 神兽保佑 
*                 代码无BUG! 
*  Author shang
*  2022-07-20 15:16:39
*/

using System.Reflection;
using Autofac;
using Autofac.Annotation;

namespace TaskScheduling.Middleware;

public static class AutofacDI
{
    private static string NameSpaceStart = "TS";

    public static void AutofaceDI(this ContainerBuilder containerBuilder)
    {
        List<Assembly> assemblies = Microsoft.Extensions.DependencyModel.DependencyContext.Default.RuntimeLibraries
            .Where(x => x.Name.StartsWith(NameSpaceStart)).Select(o => Assembly.Load(new AssemblyName(o.Name)))
            .ToList();
        assemblies.Add(typeof(Program).Assembly);
        containerBuilder.RegisterModule(
            new AutofacAnnotationModule(assemblies.ToArray()).SetAllowCircularDependencies(true));
    }
}